﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;
using Capstone_project.BussnessLogicLayer;

namespace Capstone_project
{
    public partial class frmProject : Form
    {
        SqlConnection con = Common.con;
        public frmProject()
        {
            InitializeComponent();
        }
        #region Method_load() cho datagridview project


        //private void Method_Load()
        //{
        //    Taoketnoi();
        //    SqlConnection con = new SqlConnection(ConnectionString);
        //    string select = "SELECT TASK.* FROM ASSIGN_TASK_STATUS INNER JOIN ((PROJECT INNER JOIN TASK_GROUP ON PROJECT.ID_PROJECT = TASK_GROUP.ID_PROJECT) INNER JOIN (PRIORITY INNER JOIN TASK ON PRIORITY.ID_PRIORITY = TASK.ID_PRIORITY) ON TASK_GROUP.ID_TASK_GROUP = TASK.ID_TASK_GROUP) ON ASSIGN_TASK_STATUS.ID_TASK_STATUS = TASK.ID_TASK_STATUS WHERE (((PROJECT.ID_PROJECT)='" + dgclick.CurrentRow.Cells[0].Value.ToString() + "'))";
        //    SqlDataAdapter da = new SqlDataAdapter(select,con);
        //    DataTable ds = new DataTable();
        //    da.Fill(ds);
        //    dgshow2.DataSource = ds;
        //    DataGridViewCellStyle style1 = new DataGridViewCellStyle();
        //    style1.ForeColor = Color.Blue;
        //    style1.BackColor = Color.AliceBlue;
        //    for (int i = dgshow2.RowCount - 1; i >= 0; i--)
        //    {
        //        if (i % 2 == 0) dgshow2.Rows[i].DefaultCellStyle = style1;
        //    }

        //    //do du lieu cho gridview 2
        //    string select1 = "SELECT TASK_GROUP.ID_TASK_GROUP, Sum([TASK].[COMPLETE])/Count([TASK].[ID_TASK]) AS hoanthanh FROM PROJECT INNER JOIN (TASK INNER JOIN TASK_GROUP ON TASK.ID_TASK_GROUP = TASK_GROUP.ID_TASK_GROUP) ON PROJECT.ID_PROJECT = TASK_GROUP.ID_PROJECT WHERE (((PROJECT.ID_PROJECT)='"   + dgclick.CurrentRow.Cells[0].Value.ToString() + "')) GROUP BY TASK_GROUP.ID_TASK_GROUP";
        //    SqlDataAdapter da10 = new SqlDataAdapter(select1, con);
        //    DataTable ds10 = new DataTable();
        //    da10.Fill(ds10);
        //    dgshow1.DataSource = ds10;

        //    DataGridViewCellStyle style2 = new DataGridViewCellStyle();
        //    style2.ForeColor = Color.Blue;
        //    style2.BackColor = Color.AliceBlue;
        //    for (int i = dgshow1.RowCount - 1; i >= 0; i--)
        //    {
        //        if (i % 2 == 0) dgshow1.Rows[i].DefaultCellStyle = style2;
        //    }

        //    //Dua du lieu vao Tab Control
        //    txtNameProject.DataBindings.Clear();
        //    txtIdProject.DataBindings.Clear();
        //    cmbCustomer.DataBindings.Clear();
        //    dtpStartDate.DataBindings.Clear();
        //    dtpEndDate.DataBindings.Clear();
        //    dgshow1.DataBindings.Clear();
        //    dgshow2.DataBindings.Clear();
        //    //progressBar1.DataBindings.Clear();
        //    //progressBar2.DataBindings.Clear();
        //    //progressBar3.DataBindings.Clear();
        //    //progressBar4.DataBindings.Clear();
        //    //progressBar5.DataBindings.Clear();
        //    //progressBar6.DataBindings.Clear();

        //    SqlDataAdapter adap1 = new SqlDataAdapter();
        //    SqlCommand com = new SqlCommand();
        //    DataSet ds1 = new DataSet();
        //    string sql = "SELECT * FROM CUSTOMER";
        //    com = new SqlCommand(sql, con);
        //    adap1 = new SqlDataAdapter(com);
        //    ds1 = new DataSet();
        //    adap1.Fill(ds1, "CUSTOMER");
        //    DataTable dt1 = ds1.Tables["CUSTOMER"];

        //    cmbCustomer.DataSource = dt1;
        //    cmbCustomer.DisplayMember = "NAME_CUSTOMER";
        //    cmbCustomer.ValueMember = "ID_CUSTOMER";




        //    string sqlstring = "select PROJECT.ID_PROJECT, PROJECT.NAME_PROJECT, CUSTOMER.NAME_CUSTOMER, PROJECT.START_DATE, PROJECT.END_DATE from PROJECT inner join CUSTOMER on PROJECT.ID_CUSTOMER = CUSTOMER.ID_CUSTOMER where PROJECT.ID_PROJECT='" + dgclick.CurrentRow.Cells[0].Value.ToString() + "'";

        //    SqlDataAdapter da1 = new SqlDataAdapter(sqlstring, con);
        //    DataTable table = new DataTable();
        //    da1.Fill(table);
        //    BindingSource bdsource = new BindingSource();
        //    bdsource.DataSource = table;
        //    txtIdProject.DataBindings.Add("Text", bdsource, "ID_PROJECT");
        //    txtIdProject.Enabled = false;
        //    txtNameProject.DataBindings.Add("Text", bdsource, "NAME_PROJECT");
        //    txtNameProject.Enabled = false;
        //    cmbCustomer.DataBindings.Add("Text", bdsource, "NAME_CUSTOMER");
        //    cmbCustomer.Enabled = false;
        //    dtpStartDate.DataBindings.Add("Value", bdsource, "START_DATE");
        //    dtpStartDate.Enabled = false;
        //    dtpEndDate.DataBindings.Add("Value", bdsource, "END_DATE");
        //    dtpEndDate.Enabled = false;

        //    con.Close();

        //}

        #endregion
        #region form load()
        public void frmProject_Load(object sender, EventArgs e)
        {
            if (Common.permission == "Admin" || Common.permission == "PM/PL")
            {
                groupBox2.Visible = true;
                btnEdit.Visible = true;
                btnCancel.Visible = false;
                btnDelete.Enabled = false;
                btnSave.Enabled = false;
            }
            else
            {
                groupBox2.Visible = false;
                btnAddModule.Visible = false;
                btnAddTask.Visible = false;
            }
            buidtree();
            if (frmSearchProject.abc != null)
            {
                LoadProject(frmSearchProject.abc);
            }
        }
        #endregion
        #region Sự kiện Click của gridview project
        //private void dgclick_CellClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    try
        //    {
        //        Method_Load();
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.ToString());
        //    }
        //}
        #endregion

        private void btnEdit_Click_1(object sender, EventArgs e)
        {
            btnSave.Enabled = true;
            btnDelete.Enabled = true;
            btnCancel.Visible = true;
            btnEdit.Visible = false;
            txtNameProject.Enabled = true;
            cmbCustomer.Enabled = true;
            dtpStartDate.Enabled = true;
            dtpEndDate.Enabled = true;
        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            try
            {

                if (txtIdProject.Text == "" || txtNameProject.Text == "" || dtpStartDate.Text == "" || dtpEndDate.Text == "" || cmbCustomer.Text == "")
                {
                    MessageBox.Show("You must enter all infomation");
                }

                else
                {
                    if (dtpEndDate.Value.Date <= dtpStartDate.Value.Date)
                    {
                        MessageBox.Show("End Date must bigger than start date !!!!");
                    }
                    else
                    {
                        if(con.State!=ConnectionState.Open)
                            con.Open();
                        DateTime dt1 = dtpStartDate.Value;
                        DateTime dt2 = dtpEndDate.Value;
                        SqlCommand cmd = new SqlCommand("EditProjectInfor", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@idproject",SqlDbType.NChar).Value=txtIdProject.Text;
                        cmd.Parameters.Add("@nameproject", SqlDbType.NChar).Value = txtNameProject.Text;
                        cmd.Parameters.Add("@idcustomer", SqlDbType.NChar).Value = cmbCustomer.SelectedValue;
                        cmd.Parameters.Add("@startdate", SqlDbType.NChar).Value = dtpStartDate.Text;
                        cmd.Parameters.Add("@enddate", SqlDbType.NChar).Value = dtpEndDate.Text;
                        int res = cmd.ExecuteNonQuery();
                        con.Close();
                        if (res != 0)
                        {
                            MessageBox.Show("Project updated successfully !");
                            txtNameProject.Enabled = false;
                            cmbCustomer.Enabled = false;
                            dtpStartDate.Enabled = false;
                            dtpEndDate.Enabled = false;
                            btnCancel.Visible = false;
                            btnSave.Enabled = false;
                            btnDelete.Enabled = false;
                            btnEdit.Visible = true;
                            txtNameProject.Enabled = false;
                            cmbCustomer.Enabled = false;
                            dtpEndDate.Enabled = false;
                            dtpStartDate.Enabled = false;
                            buidtree();
                        }
                    }
                }

            }
            catch
            {
                MessageBox.Show("Problem !!!");
            }
        }

        private void btnDelete_Click_1(object sender, EventArgs e)
        {
            if(con.State!=ConnectionState.Open)
                con.Open();
            try
            {
                if (MessageBox.Show("Do you want to delete this project ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    
                    string sql = "DELETE FROM PROJECT WHERE ID_PROJECT ='" + txtIdProject.Text.Trim() + "'";
                    SqlCommand com = new SqlCommand(sql, con);
                    int res = com.ExecuteNonQuery();
                    MessageBox.Show("All Data of project is deleted !");
                }
                con.Close();
                txtIdProject.Text = "";
                txtIdProject.Enabled = false;
                txtNameProject.Enabled = false;
                txtNameProject.Text = "";
                cmbCustomer.Select();
                cmbCustomer.Enabled = false;
                dtpStartDate.Value = DateTime.Today;
                dtpStartDate.Enabled = false;
                dtpEndDate.Value = DateTime.Today;
                dtpEndDate.Enabled = false;
                frmProject_Load(sender, e);
            }
            catch {
                MessageBox.Show("DELETE FAILS !!");
            }
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            btnCancel.Visible = false;
            btnSave.Enabled = false;
            btnDelete.Enabled = false;
            btnEdit.Visible = true;
            txtNameProject.Enabled = false;
            cmbCustomer.Enabled = false;
            dtpEndDate.Enabled = false;
            dtpStartDate.Enabled = false;
        }

        public void buidtree()
        {
            try
            {
                treeView1.Nodes.Clear();
                treeView1.ImageList = imageList1;
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("Select * from PROJECT", con);
                SqlDataAdapter daCategories = new SqlDataAdapter("Select * from TASK_GROUP", con);
                da.Fill(ds, "PROJECT");
                daCategories.Fill(ds, "TASKGROUP");
                ds.Relations.Add("Pro_TaskGroup", ds.Tables["PROJECT"].Columns["ID_PROJECT"], ds.Tables["TASKGROUP"].Columns["ID_PROJECT"]);
                foreach (DataRow dr in ds.Tables["PROJECT"].Rows)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = dr["NAME_PROJECT"].ToString();
                    tn.Name = dr["ID_PROJECT"].ToString();
                    
                    if (dr["ID_STATUS"].ToString().Trim() == "PST002")
                    {
                        tn.ImageIndex = 2;
                    }
                    else
                    {
                        tn.ImageIndex = 0;
                    }
                    
                    foreach (DataRow drChild in dr.GetChildRows("Pro_TaskGroup"))
                    {
                        TreeNode tn1 = new TreeNode();
                        tn1.Text = drChild["NAME_TASK_GROUP"].ToString();
                        tn1.Name = drChild["ID_TASK_GROUP"].ToString();
                        if (Common.CheckComplete(drChild["ID_TASK_GROUP"].ToString()) == true)
                        {
                            tn1.ImageIndex = 2;
                        }
                        else
                        {
                            tn1.ImageIndex = 1;
                        }
                        tn.Nodes.Add(tn1);
                    }
                    treeView1.Nodes.Add(tn);
                }
            }
            catch
            {
                MessageBox.Show("Problem !!!");
            }
        }

        private DataTable GetTreeViewData()
        {
            if (con.State != ConnectionState.Open)
                con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM PROJECT", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataTable dtbl = new DataTable();
            ad.Fill(dtbl);
            con.Close();
            return dtbl;
        }

        DataView GetThreads(DataTable discuss)
        {
            DataView view = new DataView(discuss);
            //view.RowFilter = "[PARENT]=1";
            return view;
        }

        public void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                control.Visible = true;

                TreeNode node1 = treeView1.SelectedNode;
                if (node1.Level == 0)
                {
                    frmAddTask.idtaskgroup = null;
                    frmCreateNewTaskgroup.idproject = node1.Name;
                    frmAddTask.idproject = node1.Name;
                    string sqlProjectstatus = "SELECT     ID_STATUS"
                    +"  FROM         dbo.PROJECT"
                    +"  WHERE     (ID_PROJECT = '"+node1.Name+"')";
                    if (con.State != ConnectionState.Open)
                        con.Open();
                    SqlCommand cmd = new SqlCommand(sqlProjectstatus,con);
                    SqlDataAdapter adap = new SqlDataAdapter(cmd);
                    DataTable dtble=new DataTable();
                    adap.Fill(dtble);
                    con.Close();
                        if (Common.CountModuleofProject(node1.Name) > 0)
                        {
                            btnAddModule.Enabled = true;
                            btnAddTask.Enabled = true;
                        }
                        else
                        {
                            btnAddTask.Enabled = false;
                            btnAddModule.Enabled = true;
                        }
                }
                else if(node1.Level==1)
                {
                    frmAddTask.idtaskgroup = node1.Name;
                    frmAddTask.idproject = node1.Parent.Name;
                    btnAddModule.Enabled = false;
                    btnAddTask.Enabled = true;
                }

                

                if (con.State != ConnectionState.Open)
                    con.Open();

                //Dua du lieu vao Tab Control
                txtNameProject.DataBindings.Clear();
                txtIdProject.DataBindings.Clear();
                cmbCustomer.DataBindings.Clear();
                dtpStartDate.DataBindings.Clear();
                dtpEndDate.DataBindings.Clear();
                //dgshow1.DataBindings.Clear();
                dgshow2.DataBindings.Clear();

                SqlDataAdapter adap1 = new SqlDataAdapter();
                SqlCommand com = new SqlCommand();
                DataSet ds1 = new DataSet();
                string sql = "SELECT * FROM CUSTOMER";
                com = new SqlCommand(sql, con);
                adap1 = new SqlDataAdapter(com);
                ds1 = new DataSet();
                adap1.Fill(ds1, "CUSTOMER");
                DataTable dt1 = ds1.Tables["CUSTOMER"];

                cmbCustomer.DataSource = dt1;
                cmbCustomer.DisplayMember = "NAME_CUSTOMER";
                cmbCustomer.ValueMember = "ID_CUSTOMER";

                DataGridViewCellStyle style2 = new DataGridViewCellStyle();
                style2.ForeColor = Color.Blue;
                style2.BackColor = Color.AliceBlue;
                if (node1.Level == 0)
                {
                    LoadProject(node1.Name);
                }
                else
                {
                    string select = "SELECT dbo.TASK.ID_TASK, dbo.TASK.NAME_TASK, dbo.TASK_GROUP.NAME_TASK_GROUP, dbo.PRIORITY.NAME_PRIORITY, dbo.ASSIGN_TASK_STATUS.STATUS, "
            + "      dbo.TASK.TIMES, dbo.TASK.COMPLETE"
            + " FROM         dbo.TASK INNER JOIN"
            + "      dbo.TASK_GROUP ON dbo.TASK.ID_TASK_GROUP = dbo.TASK_GROUP.ID_TASK_GROUP INNER JOIN"
            + "      dbo.PROJECT ON dbo.TASK_GROUP.ID_PROJECT = dbo.PROJECT.ID_PROJECT INNER JOIN"
            + "      dbo.PRIORITY ON dbo.TASK.ID_PRIORITY = dbo.PRIORITY.ID_PRIORITY INNER JOIN"
            + "      dbo.ASSIGN_TASK_STATUS ON dbo.TASK.ID_TASK_STATUS = dbo.ASSIGN_TASK_STATUS.ID_TASK_STATUS"
            + " WHERE (((TASK_GROUP.ID_TASK_GROUP)='" + node1.Name + "'))";
                    SqlDataAdapter da = new SqlDataAdapter(select, con);
                    DataTable ds = new DataTable();
                    da.Fill(ds);
                    dgshow2.DataSource = ds;
                    DataGridViewCellStyle style1 = new DataGridViewCellStyle();
                    style1.ForeColor = Color.Blue;
                    style1.BackColor = Color.AliceBlue;
                    for (int i = dgshow2.RowCount - 1; i >= 0; i--)
                    {
                        if (i % 2 == 0) dgshow2.Rows[i].DefaultCellStyle = style1;
                    }
                    string select1 = "SELECT     dbo.TASK_GROUP.ID_TASK_GROUP, dbo.TASK_GROUP.NAME_TASK_GROUP"
            + " FROM         dbo.TASK_GROUP INNER JOIN"
            + "      dbo.PROJECT ON dbo.TASK_GROUP.ID_PROJECT = dbo.PROJECT.ID_PROJECT"
            + " WHERE (((PROJECT.ID_PROJECT)='" + node1.Name + "')) GROUP BY TASK_GROUP.ID_TASK_GROUP,TASK_GROUP.NAME_TASK_GROUP";
                    SqlDataAdapter da10 = new SqlDataAdapter(select1, con);
                    DataTable ds10 = new DataTable();
                    da10.Fill(ds10);
                    //dgshow1.DataSource = ds10;

                    string sqlstring = "select PROJECT.ID_PROJECT, PROJECT.NAME_PROJECT, CUSTOMER.NAME_CUSTOMER, PROJECT.START_DATE, PROJECT.END_DATE from PROJECT inner join CUSTOMER on PROJECT.ID_CUSTOMER = CUSTOMER.ID_CUSTOMER where PROJECT.ID_PROJECT='" + node1.Parent.Name + "'";

                    SqlDataAdapter da1 = new SqlDataAdapter(sqlstring, con);
                    DataTable table = new DataTable();
                    da1.Fill(table);
                    BindingSource bdsource = new BindingSource();
                    bdsource.DataSource = table;
                    txtIdProject.DataBindings.Add("Text", bdsource, "ID_PROJECT");
                    txtIdProject.Enabled = false;
                    txtNameProject.DataBindings.Add("Text", bdsource, "NAME_PROJECT");
                    txtNameProject.Enabled = false;
                    cmbCustomer.DataBindings.Add("Text", bdsource, "NAME_CUSTOMER");
                    cmbCustomer.Enabled = false;
                    dtpStartDate.DataBindings.Add("Value", bdsource, "START_DATE");
                    dtpStartDate.Enabled = false;
                    dtpEndDate.DataBindings.Add("Value", bdsource, "END_DATE");
                    dtpEndDate.Enabled = false;

                }
                con.Close();

            }
            catch
            {
                MessageBox.Show("Problem!!!");
            }
        }

        private void btnAddModule_Click(object sender, EventArgs e)
        {
            frmCreateNewTaskgroup addmodule = new frmCreateNewTaskgroup();
            addmodule.Show();
        }

        private void btnAddTask_Click(object sender, EventArgs e)
        {
            // Se gan mot bien status vao ben from kia, voi gia tri cua node dang duoc chon
            // O Form ben  kia: 
            //Formload: Bien nay la tham so dau vao de load combobox
            frmAddTask frmAddtask = new frmAddTask();
            frmAddtask.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Crystal a = new Crystal();
            a.ShowDialog();
        }

        private void treeView1_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Right)
            {
                // Select the clicked node
                treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);

                if(treeView1.SelectedNode != null)
                {
                    //InitializeMenuTreeView(treeView1.SelectedNode);
                }
            }
        }
    //    public void InitializeMenuTreeView(TreeNode nodeabc)
    //    {
    //        // Create the TreeView. 
    //        TreeView menuTreeView = new TreeView();
    //        menuTreeView.Size = new Size(200, 200);

    //        //// Create the root node. 
    //        //TreeNode docNode = new TreeNode("Documents");

    //        //// Add some additional nodes. 
    //        //docNode.Nodes.Add("phoneList.doc");
    //        //treeView1.Nodes.Add("resume.doc");

    //        //// Add the root nodes to the 
    //        //menuTreeView.Nodes.Add(docNode);

    //        // Create the 
    //        ContextMenuStrip docMenu = new ContextMenuStrip();

    //        //Create some menu items. 
    //        ToolStripMenuItem openLabel = new ToolStripMenuItem();
    //        openLabel.Text = "Open";
    //        ToolStripMenuItem deleteLabel = new ToolStripMenuItem();
    //        deleteLabel.Text = "Delete";
    //        ToolStripMenuItem renameLabel = new ToolStripMenuItem();
    //        renameLabel.Text = "Rename";

    //        //Add the menu items to the menu. 
    //        docMenu.Items.AddRange(new ToolStripMenuItem[]{openLabel,
    //        deleteLabel, renameLabel});

    //        // Set the ContextMenuStrip property to the 
    //        nodeabc.ContextMenuStrip = docMenu;

    //        // Add the TreeView to the form. 
    //        treeView1.Controls.Add(docMenu);
    //}
        public void LoadProject(string idpro)
        {
            txtNameProject.DataBindings.Clear();
            txtIdProject.DataBindings.Clear();
            cmbCustomer.DataBindings.Clear();
            dtpStartDate.DataBindings.Clear();
            dtpEndDate.DataBindings.Clear();
            dgshow2.DataBindings.Clear();
            string select = "SELECT     dbo.TASK.ID_TASK, dbo.TASK.NAME_TASK, dbo.TASK_GROUP.NAME_TASK_GROUP, dbo.PRIORITY.NAME_PRIORITY, dbo.ASSIGN_TASK_STATUS.STATUS, "
                    + "      dbo.TASK.TIMES, dbo.TASK.COMPLETE"
                    + " FROM         dbo.TASK INNER JOIN"
                    + "      dbo.TASK_GROUP ON dbo.TASK.ID_TASK_GROUP = dbo.TASK_GROUP.ID_TASK_GROUP INNER JOIN"
                    + "      dbo.PROJECT ON dbo.TASK_GROUP.ID_PROJECT = dbo.PROJECT.ID_PROJECT INNER JOIN"
                    + "      dbo.PRIORITY ON dbo.TASK.ID_PRIORITY = dbo.PRIORITY.ID_PRIORITY INNER JOIN"
                    + "      dbo.ASSIGN_TASK_STATUS ON dbo.TASK.ID_TASK_STATUS = dbo.ASSIGN_TASK_STATUS.ID_TASK_STATUS"
                    + " WHERE (((PROJECT.ID_PROJECT)='" + idpro + "'))";
            SqlDataAdapter da = new SqlDataAdapter(select, con);
            DataTable ds = new DataTable();
            da.Fill(ds);
            dgshow2.DataSource = ds;
            DataGridViewCellStyle style1 = new DataGridViewCellStyle();
            style1.ForeColor = Color.Blue;
            style1.BackColor = Color.AliceBlue;
            for (int i = dgshow2.RowCount - 1; i >= 0; i--)
            {
                if (i % 2 == 0) dgshow2.Rows[i].DefaultCellStyle = style1;
            }
            string select1 = "SELECT     dbo.TASK_GROUP.ID_TASK_GROUP, dbo.TASK_GROUP.NAME_TASK_GROUP"
       + " FROM         dbo.TASK_GROUP INNER JOIN"
       + "      dbo.PROJECT ON dbo.TASK_GROUP.ID_PROJECT = dbo.PROJECT.ID_PROJECT"
       + " WHERE (((PROJECT.ID_PROJECT)='" + idpro + "')) GROUP BY TASK_GROUP.ID_TASK_GROUP,TASK_GROUP.NAME_TASK_GROUP";
            SqlDataAdapter da10 = new SqlDataAdapter(select1, con);
            DataTable ds10 = new DataTable();
            da10.Fill(ds10);
            //dgshow1.DataSource = ds10;

            string sqlstring = "select PROJECT.ID_PROJECT, PROJECT.NAME_PROJECT, CUSTOMER.NAME_CUSTOMER, PROJECT.START_DATE, PROJECT.END_DATE from PROJECT inner join CUSTOMER on PROJECT.ID_CUSTOMER = CUSTOMER.ID_CUSTOMER where PROJECT.ID_PROJECT='" + idpro + "'";

            SqlDataAdapter da1 = new SqlDataAdapter(sqlstring, con);
            DataTable table = new DataTable();
            da1.Fill(table);
            BindingSource bdsource = new BindingSource();
            bdsource.DataSource = table;
            txtIdProject.DataBindings.Add("Text", bdsource, "ID_PROJECT");
            txtIdProject.Enabled = false;
            txtNameProject.DataBindings.Add("Text", bdsource, "NAME_PROJECT");
            txtNameProject.Enabled = false;
            cmbCustomer.DataBindings.Add("Text", bdsource, "NAME_CUSTOMER");
            cmbCustomer.Enabled = false;
            dtpStartDate.DataBindings.Add("Value", bdsource, "START_DATE");
            dtpStartDate.Enabled = false;
            dtpEndDate.DataBindings.Add("Value", bdsource, "END_DATE");
            dtpEndDate.Enabled = false;
        }

        private void btnCloseproject_Click(object sender, EventArgs e)
        {
            TreeNode node1 = treeView1.SelectedNode;
            string sql = "update PROJECT SET ID_STATUS='PST002' where ID_PROJECT='"+node1.Name+"'";
            if (con.State != ConnectionState.Open)
                    con.Open();
            SqlCommand cmd = new SqlCommand(sql,con);
            int kq = cmd.ExecuteNonQuery();
            con.Close();
            if (kq != 0)
            {
                MessageBox.Show("Project is closed.");
                frmProject_Load(null, null);
            }
        }

      }
}
 